#include"stack.h"


void StackInit(ST* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->capacity = ps->top = 0;
}
void StackDestroy(ST* ps)
{
	assert(ps);
	free(ps);
	ps->a = NULL;
	ps->capacity = ps->top = 0;
}
void StackPush(ST* ps, STDataType x)
{
	assert(ps);
	if (ps->capacity == ps->top)
	{
		int NewCapacity = (ps->capacity == 0) ? 4 : ps->capacity * 2;
		STDataType* temp = (STDataType*)realloc(ps->a, NewCapacity * sizeof(STDataType));
		if (temp == NULL)
		{
			perror("realloc fail");
			exit(-1);
		}
		ps->capacity = NewCapacity;
		ps->a = temp;
	}
	ps->a[ps->top] = x;
	ps->top++;
}
void StackPop(ST* ps)
{
	assert(ps);
	assert(!StackEmpyt(ps));
	ps->top--;
}
STDataType StackTop(ST* ps)
{
	assert(ps);
	assert(!StackEmpyt(ps));
	return ps->a[ps->top - 1];
}
int StackEmpyt(ST* ps)
{
	assert(ps);
	return ps->top == 0;
}
int STackSize(ST* ps) 
{
	assert(ps);
	return ps->top;
}
